$(function()
{
	$("#satuan_harga").change(function()
	{
		cartedit_calculation();
	});
	$("#satuan_unit").change(function()
	{
		cartedit_calculation();
	});
	$("#form_cartedit").validate(
	{
		rules:
		{
			satuan_harga: "required",
			satuan_unit: "required"
		},
		messages:
		{
			satuan_harga: "Anda harus memilih satuan harga",
			satuan_unit: "Anda harus memilih satuan unit"
		}
	});
	$.validator.addMethod("notIncludeZero",function(value, element, param)
	{
		if(value > param)
			return true;
		return false;
	}, "Jumlah harus lebih dari 0"
	);
	$("#form_shade").validate(
	{
		rules:
		{
			shade: "required",
			jumlah:
			{
				required: true,
				number: true,
				notIncludeZero: 0
			}
		},
		messages:
		{
			shade: "Shade wajib diisi",
			jumlah:
			{
				required: "Jumlah wajib diisi",
				number: "Jumlah harus diisi dengan angka"
			}
		}
	});
});
Number.prototype.formatMoney = function(c, d, t)
{
	var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
	return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};
function roundNumber(num,dec)
{
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
function cartedit_calculation()
{
	var jumlah_unit = $("#jumlah_unit").val();
	var satuan_unit = $("#satuan_unit").val();
	var konversi_unit = $("#konversi_"+satuan_unit).val();
	if(konversi_unit == undefined)
		konversi_unit = 0;
	var jumlah_harga = jumlah_unit * konversi_unit;
	$("#jumlah_harga").val(jumlah_harga.formatMoney(round_money,'.',','));
	var satuan_harga = $("#satuan_harga").val();
	var harga = $("#harga_"+satuan_harga).val();
	if(harga == undefined)
		harga = 0;
	harga = parseFloat(harga);
	harga = roundNumber(harga,round_money);
	$("#harga").val(harga.formatMoney(round_money,'.',','));
	var diskon = $("#diskon").val();
	var netto = harga - (harga * diskon / 100);
	$("#netto").val(netto.formatMoney(round_money,'.',','));
	var total = jumlah_harga * netto;
	$("#total").val(total.formatMoney(round_money,'.',','));
}